﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeCamp12.Domain;
using System.Data.SqlClient;
using System.Configuration;
using log4net;

namespace CodeCamp12.Domain
{
    public class TrackDataManagement
    {
        private ConnectionStringProvider connString = new ConnectionStringProvider();
        private ILog log = LogManager.GetLogger("CodeCamp12.DAL");
       
        /// <summary>
        /// Inserts new track in the database
        /// </summary>
        /// <param name="track"></param>
        public void InsertTrack(Track track)
        {
            var conn = connString.GetConnection();
            var comm = new SqlCommand("InsertTrack", conn) {CommandType = System.Data.CommandType.StoredProcedure};
            comm.Parameters.AddWithValue("@TrackName", track.TrackName);
            comm.Parameters.AddWithValue("@TrackColor", track.TrackColor);
            comm.Parameters.AddWithValue("@Description", track.Description);
            comm.Parameters.AddWithValue("@EventID", track.EventID);
            try
            {
                conn.Open();
                comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }
            finally
            {
                conn.Close();
            }
        }

        /// <summary>
        /// Deletes track from the database
        /// </summary>
        /// <param name="track"></param>
        public void DeleteTrack(Track track)
        {
            var conn = connString.GetConnection();
            var comm = new SqlCommand("DeleteTrack", conn) { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@TrackID", track.TrackID);
            try
            {
                conn.Open();
                comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }
            finally
            {
                conn.Close();
            }
        }

        /// <summary>
        /// Returns all created tracks for event
        /// </summary>
        /// <param name="eventId"></param>
        /// <returns></returns>
        public List<Track> GetAllTracks(Guid eventId)
        {
            List<Track> trackList = null;
            var conn = connString.GetConnection();
            var comm = new SqlCommand("GetAllTracksForEvent", conn) { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@id", eventId);
            try
            {
                conn.Open();
                var reader = comm.ExecuteReader();
                while(reader.Read())
                {
                    if(trackList == null)
                    {
                        trackList = new List<Track>();
                    }
                    var track = new Track
                                    {
                                        TrackID = new Guid(reader["TrackID"].ToString()),
                                        TrackName = reader["TrackName"].ToString(),
                                        TrackColor = reader["TrackColor"].ToString(),
                                        EventID = new Guid(reader["EventID"].ToString()),
                                        Description = reader["Description"].ToString()
                                    };
                    trackList.Add(track);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }
            finally
            {
                conn.Close();
            }
            return trackList;
        } 
    }
}
